﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AB
{
    public partial class ExpView : Form
    {
        TrialBlock trialBlock;

        Timer timerOn;
        Timer timerOff;

        public ExpView(TrialBlock trialBlock)
        {
            InitializeComponent();
            hideInput();
            actionButton.Enabled = true;
            actionButton.Text = "Start Trials";
            trialText.Text = trialBlock.getNumTrials().ToString();
            this.trialBlock = trialBlock;
        }

        private void actionButton_Click(object sender, EventArgs e)
        {

            trialBlock.setSubjectName(subjectText.Text);
            trialText.Enabled = false;
            controlRadio.Enabled = false;
            expRadio.Enabled = false;
            subjectText.TabStop = false;

            // Not first trial
            if (trialBlock.trialNumber != 0)
            {
                validateInput();
            }
            else // First trial
            {
                trialBlock.setNumTrials(Convert.ToInt32(trialText.Text));
                trialBlock.startTime = DateTime.Now;
            }

            // More trials
            if (trialBlock.nextTrial())
            {
                newTrial();
            }
            else // Done with trials
            {
                trialBlock.endTime = DateTime.Now;
                actionButton.Text = "You're Done";
                actionButton.Enabled = false;
                trialBlock.getResults();
            }
            
        }

        private void newTrial()
        {

            trialBlock.currentTrial.startTrial();

            hideInput();

            timerOn = new Timer();
            timerOn.Tick += new EventHandler(this.timerOn_Tick);
            timerOn.Interval = trialBlock.currentTrial.getSOATime();
            timerOn.Start();

            System.Threading.Thread.Sleep(trialBlock.currentTrial.getISITime());

            timerOff = new Timer();
            timerOff.Tick += new EventHandler(this.timerOff_Tick);
            timerOff.Interval = trialBlock.currentTrial.getSOATime();
            timerOff.Start();
        }

        private void validateInput()
        {
            trialBlock.currentTrial.validateInput(targetText.Text.ToUpper(), getProbeResult());
        }

        private void timerOn_Tick(object sender, EventArgs e)
        {
            if (trialBlock.currentTrial.anotherStimulus())
            {
                stimulusLabel.ForeColor = trialBlock.currentTrial.getColor();
                stimulusLabel.Text = trialBlock.currentTrial.getNextStim();
            }
            else
            {
                stimulusLabel.Text = "";
                timerOn.Stop();
                timerOff.Stop();
                showInput();
            }  
        }

        private void timerOff_Tick(object sender, EventArgs e)
        {
            stimulusLabel.Text = "";
        }

        private void hideInput()
        {
            actionButton.Enabled = false;
            probePresentLabel.Hide();
            targetLabel.Hide();
            targetText.Hide();
            targetText.Clear();
            probeYes.Hide();
            probeYes.Checked = false;
            probeNo.Hide();
            probeNo.Checked = false;
            probeGroup.Hide();
        }

        private void showInput()
        {
            trialBlock.currentTrial.startTime = DateTime.Now;
            actionButton.Text = "Next Trial";
            probePresentLabel.Show();
            targetLabel.Show();
            targetText.Show();
            probeYes.Show();
            probeNo.Show();
            probeGroup.Show();
            if (controlRadio.Checked == true)
            {
                trialBlock.blockType = "control";
                targetText.Text = "N/A";
                //probeGroup.TabIndex = 5;
            }
            else
            {
                trialBlock.blockType = "experimental";
                targetText.Select();
                targetText.Focus();
                //targetText.TabIndex = 5;
            }
            stimulusLabel.Text = "+";
        }

        private Boolean getProbeResult()
        {
            Boolean result = false;
            if (probeYes.Checked == true)
            {
                result = true;
            }
            if (probeNo.Checked == true)
            {
                result = false;
            }
            return result;
        }

        private void targetText_TextChanged(object sender, EventArgs e)
        {
            responseEntered();
        }
        private void responseEntered()
        {
            if ((targetText.Text.Length > 0) && (probeYes.Checked || probeNo.Checked))
            {
                actionButton.Enabled = true;
            }
        }

        private void probeYes_CheckedChanged(object sender, EventArgs e)
        {
            responseEntered();
        }

        private void probeNo_CheckedChanged(object sender, EventArgs e)
        {
            responseEntered();
        }

        private void infoEntered()
        {
            if ((subjectText.Text.Length > 0) && (controlRadio.Checked || expRadio.Checked))
            {
                showExperiment();
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            infoEntered();
        }
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            infoEntered();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            infoEntered();
        }

        private void showExperiment()
        {
            //Disable subject stuff
            actionButton.Show();
            stimBorder.Show();
            stimulusLabel.Show();
            instructionLabel2.Show();
            instructionLabel.Text = "You will be presented with a stream of black and white letters. "+ "\r\n" + "Your task is to: ";
            if (expRadio.Checked == true)
            {
                instructionLabel2.Text = "1.Identify the White Letter. " + "\r\n" + "2. Determine if there is a black 'X' in the stream";
            }
            else if (controlRadio.Checked == true)
            {
                instructionLabel2.Text = "1. Determine if there is a black 'X' in the stream";
            }
        }

        private void instructionLabel_Click(object sender, EventArgs e)
        {

        }



    }
}
